package org.ztr.yanai.blog.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.dto.LoginDTO;
import org.ztr.yanai.blog.dto.RegisterDTO;
import org.ztr.yanai.blog.dto.UserDTO;
import org.ztr.yanai.blog.entity.User;
import org.ztr.yanai.blog.service.UserService;
import org.ztr.yanai.blog.utils.EntityDtoConverter;
import org.ztr.yanai.blog.utils.Result;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 14:30
 * @Description: 认证控制器，处理登录、注册等认证相关操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/auth")
@Tag(name = "认证管理", description = "用户登录、注册等认证相关接口")
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "注册新用户账号")
    public Result<UserDTO> register(@Valid @RequestBody RegisterDTO registerDTO) {
        User user = userService.register(registerDTO);
        return Result.success(EntityDtoConverter.toUserDTO(user));
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录并获取Token")
    public Result<Map<String, Object>> login(@Valid @RequestBody LoginDTO loginDTO) {
        String token = userService.login(loginDTO);
        
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("tokenType", "Bearer");
        
        return Result.success(data);
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出", description = "用户退出登录")
    public Result<Void> logout() {
        // TODO: 实现登出逻辑，清除Token
        return Result.success();
    }

    @GetMapping("/info")
    @Operation(summary = "获取当前用户信息", description = "根据Token获取当前登录用户信息")
    public Result<UserDTO> getCurrentUserInfo() {
        // TODO: 从Token中获取当前用户ID
        // Long userId = getCurrentUserId();
        // User user = userService.getUserById(userId);
        // return Result.success(EntityDtoConverter.toUserDTO(user));
        return Result.success(null);
    }
}
